package main

import "fmt"

// 工厂模式定义一个创建对象的接口，让子类决定实例化哪个类。

// Shape 形状接口
type Shape interface {
	Draw()
}

// Circle 圆形结构体
type Circle struct{}

func (c *Circle) Draw() {
	fmt.Println("Drawing a circle.")
}

// Rectangle 矩形结构体
type Rectangle struct{}

func (r *Rectangle) Draw() {
	fmt.Println("Drawing a rectangle.")
}

// ShapeFactory 形状工厂
type ShapeFactory struct{}

// CreateShape 创建形状实例
func (sf *ShapeFactory) CreateShape(shapeType string) Shape {
	switch shapeType {
	case "circle":
		return &Circle{}
	case "rectangle":
		return &Rectangle{}
	default:
		return nil
	}
}

func main() {
	factory := &ShapeFactory{}

	circle := factory.CreateShape("circle")
	circle.Draw()

	rectangle := factory.CreateShape("rectangle")
	rectangle.Draw()
}
